package facade.base;

import facade.AppContext;
import facade.open.request.base.AdminBusinessRequest;
import models.base.AdminBusiness;
import models.base.AdminBusinessOperate;
import models.base.AdminMenu;
import models.base.AdminUser;
import org.slf4j.LoggerFactory;

import java.util.Date;

import static helper.BeanHelper.safeSetValue;

public class AdminBusinessFacade {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AdminBusinessFacade.class);


    /**
     * 创建业务 、功能
     * @param businessRequest
     * @return
     */
    public static AdminBusiness create(AdminBusinessRequest businessRequest){
        AdminBusiness business = new AdminBusiness();
        safeSetValue(businessRequest.name, value -> business.name = value );
        safeSetValue(businessRequest.controller, value -> business.controller = value );
        safeSetValue(businessRequest.path, value -> business.path = value );
        if(businessRequest.showOrder == null) businessRequest.showOrder = 0;
        safeSetValue(businessRequest.showOrder, value -> business.showOrder = value );
        safeSetValue(businessRequest.remark, value -> business.remark = value );

        business.save();

        if(businessRequest.menuList != null && businessRequest.menuList.size()>0){
            for (AdminBusinessOperate operate : businessRequest.menuList) {
                if(operate.menuId != null) {
                    AdminMenu menu = AdminMenu.findOne(operate.menuId);
                    if (menu != null) {
                        operate.menuItem = menu;
                        operate.business = business;
                        operate.save();
                    }
                }
            }
        }

        return business ;
    }

    /**
     * 修改
     * @param business 业务
     * @param businessRequest 请求提交的数据
     * @return
     */
    public  static AdminBusiness update(AdminBusiness business , AdminBusinessRequest businessRequest){
        AdminUser loginUser = AppContext.currentUser(); //获取登录用户
        safeSetValue(businessRequest.name, value -> business.name = value );
        safeSetValue(businessRequest.controller, value -> business.controller = value );
        safeSetValue(businessRequest.path, value -> business.path = value );
        if(businessRequest.showOrder == null) businessRequest.showOrder = 0;
        safeSetValue(businessRequest.showOrder, value -> business.showOrder = value );
        safeSetValue(businessRequest.remark, value -> business.remark = value );

        business.modifyUser = loginUser ;
        business.modifyAt = new Date() ;
        business.save() ;

        AdminBusinessOperate.deleteByBusinessId(business.id);
        if(businessRequest.menuList != null && businessRequest.menuList.size()>0){
            for (AdminBusinessOperate operate : businessRequest.menuList) {
                if(operate.menuId != null) {
                    AdminMenu menu = AdminMenu.findOne(operate.menuId);
                    if (menu != null) {
                        operate.menuItem = menu;
                        operate.business = business;
                        operate.save();
                    }
                }
            }
        }
        return business ;

    }

    private static String getNewCode(){
        AdminBusiness business = AdminBusiness.findLastOne();
        if(business != null){
            String code = business.code.replace("AB","");
            Integer intCode = Integer.valueOf(code);
            intCode+=1;
            return "AB"+intCode;
        }
        return "AB1000";
    }
}
